// pages/my/orderDetail/orderDetail.js
const utils = require("../../../utils/utils.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: {},
        id: '',
        pdbottom: 0,
        btnHeight: 0,
        firstOpen: 0
    },
    wxContant(e) {
        var url = e.currentTarget.dataset.url;
        var id = e.currentTarget.dataset.id;
        var code = e.currentTarget.dataset.code;
        wx.openCustomerServiceChat({
            extInfo: {
                url: url
            },
            corpId: id,
            showMessageCard: true,
            sendMessageTitle: '电子证件照订单详情',
            sendMessagePath: '/pages/my/electronicDetail/electronicDetail.html?id=' + code,
            success(res) {}
        })
    },
    printAgain(e) {
        var id = e.currentTarget.dataset.id;
        wx.showLoading({
            title: '请稍候',
        })
        utils.request('Member/taskAgain', {
            taskId: id
        }, function (res) {
            if (res.data.code == 1) {
                wx.navigateTo({
                    url: '../../print/paySuccess/paySuccess?type=1',
                })
            } else {
                wx.hideLoading()
                utils.toast(res.data.msg)
            }
        })
    },
    prive(e) {
        var url = e.currentTarget.dataset.url;
        var office = e.currentTarget.dataset.office;
        wx.showLoading()
        if (office == 1) {
            wx.hideLoading()
            wx.navigateTo({
                url: '/pages/my/pdfsee/pdfsee?url=' + url
            })
        } else {
            wx.hideLoading()
            wx.previewImage({
                current: url, // 当前显示图片的http链接
                urls: [url] // 需要预览的图片http链接列表
            })

        }
    },
    getIndex(id) {
        var that = this;
        utils.request('idPhotoOrder/detail/' + id, {}, function (res) {
            if (res.data.code == 200) {
                if (res.data.data.orderStatus == 1) {
                    that.setData({
                        btnHeight: '100rpx'
                    })
                }
                that.setData({
                    list: res.data.data,
                    // payTypeList: res.data.data.payTypeList
                })

            } else {
                wx.hideLoading()
                utils.toast(res.data.msg)
            }
        })
    },
    getPhoto(e) {
        var that = this;
        var index = e.currentTarget.dataset.index;
        var tag = e.currentTarget.dataset.tag;
        if (index == 0) {
            //保存
            if (utils.is_define(that.data.fileUrl) && utils.is_define(that.data.fileUrlSingle)) {
                if (tag == 0) {
                    wx.showLoading()
                    //单图保存
                    wx.downloadFile({
                        url: that.data.fileUrlSingle,
                        success(res) {
                            if (res.statusCode === 200) {
                                wx.saveImageToPhotosAlbum({
                                    filePath: res.tempFilePath,
                                    success(res) {
                                        wx.hideLoading()
                                        wx.showToast({
                                            title: '保存成功',
                                            icon: 'success',
                                            duration: 2000
                                        })
                                    },
                                    fail() {
                                        wx.hideLoading()
                                        wx.showToast({
                                            title: '失败，请重试',
                                            icon: 'error',
                                            duration: 2000
                                        })
                                    }
                                })
                            }
                        },
                        fail(err) {
                            console.log(err)
                        }
                    })
                }
                if (tag == 1) {
                    var downUrl = '';
                    wx.showActionSheet({
                        itemList: ['无边距', '有边距'],
                        success(res) {
                            if (res.tapIndex == 0) {
                                downUrl = that.data.fileUrlMargin
                            }
                            if (res.tapIndex == 1) {
                                downUrl = that.data.fileUrl
                            }
                            wx.showLoading()
                            //拼版保存
                            wx.downloadFile({
                                url: downUrl,
                                success(res) {
                                    if (res.statusCode === 200) {
                                        wx.saveImageToPhotosAlbum({
                                            filePath: res.tempFilePath,
                                            success(res) {
                                                wx.hideLoading()
                                                wx.showToast({
                                                    title: '保存成功',
                                                    icon: 'success',
                                                    duration: 2000
                                                })
                                            },
                                            fail() {
                                                wx.hideLoading()
                                                wx.showToast({
                                                    title: '失败，请重试',
                                                    icon: 'error',
                                                    duration: 2000
                                                })
                                            }
                                        })
                                    }
                                }
                            })
                        }
                    })
                }
            } else {
                wx.showLoading({
                    title: '请稍候',
                })
                utils.request('idPhotoOrder/getOrder', {
                    orderNo: that.data.id
                }, function (res) {
                    if (res.data.code == 200) {
                        that.setData({
                            fileUrl: res.data.data.fileUrl,
                            fileUrlSingle: res.data.data.fileUrlSingle,
                            fileUrlMargin: res.data.data.fileUrlMargin,
                        })
                        if (tag == 0) {
                            //单图保存
                            wx.downloadFile({
                                url: that.data.fileUrlSingle,
                                success(res) {
                                    if (res.statusCode === 200) {
                                        wx.saveImageToPhotosAlbum({
                                            filePath: res.tempFilePath,
                                            success(res) {
                                                wx.hideLoading()
                                                wx.showToast({
                                                    title: '保存成功',
                                                    icon: 'success',
                                                    duration: 2000
                                                })
                                            },
                                            fail() {
                                                wx.hideLoading()
                                                wx.showToast({
                                                    title: '失败，请重试',
                                                    icon: 'error',
                                                    duration: 2000
                                                })
                                            }
                                        })
                                    }
                                }
                            })
                        }
                        if (tag == 1) {
                            wx.hideLoading()
                            var downUrl = '';
                            wx.showActionSheet({
                                itemList: ['无边距', '有边距'],
                                success(res) {
                                    if (res.tapIndex == 0) {
                                        downUrl = that.data.fileUrlMargin
                                    }
                                    if (res.tapIndex == 1) {
                                        downUrl = that.data.fileUrl
                                    }
                                    wx.showLoading()
                                    //拼版保存
                                    wx.downloadFile({
                                        url: downUrl,
                                        success(res) {
                                            if (res.statusCode === 200) {
                                                wx.saveImageToPhotosAlbum({
                                                    filePath: res.tempFilePath,
                                                    success(res) {
                                                        wx.hideLoading()
                                                        wx.showToast({
                                                            title: '保存成功',
                                                            icon: 'success',
                                                            duration: 2000
                                                        })
                                                    },
                                                    fail() {
                                                        wx.hideLoading()
                                                        wx.showToast({
                                                            title: '失败，请重试',
                                                            icon: 'error',
                                                            duration: 2000
                                                        })
                                                    }
                                                })
                                            }
                                        }
                                    })
                                }
                            })
                        }
                    } else {
                        wx.hideLoading()
                        utils.toast(res.data.msg)
                    }
                })
            }
        }
        if (index == 1) {
            //预览
            if (utils.is_define(that.data.pfileUrl) && utils.is_define(that.data.pfileUrlSingle)) {
                if (tag == 0) {
                    //单图预览
                    wx.previewImage({
                        current: that.data.pfileUrlSingle, // 当前显示图片的http链接
                        urls: [that.data.pfileUrlSingle] // 需要预览的图片http链接列表
                    })
                }
                if (tag == 1) {
                    //拼版预览
                    wx.previewImage({
                        current: that.data.pfileUrl, // 当前显示图片的http链接
                        urls: [that.data.pfileUrl] // 需要预览的图片http链接列表
                    })
                }
            } else {
                wx.showLoading()
                var data = {
                    orderNo: that.data.id
                }
                utils.request('idPhotoOrder/getOrder', data, function (res) {
                    if (res.data.code == 200) {
                        var path = res.data.data.fileUrl
                        var path1 = res.data.data.fileUrlSingle
                        wx.hideLoading({
                            success: (res) => {
                                that.setData({
                                    pfileUrl: path,
                                    pfileUrlSingle: path1
                                })
                                if (tag == 0) {
                                    //单图预览
                                    wx.previewImage({
                                        current: that.data.pfileUrlSingle, // 当前显示图片的http链接
                                        urls: [that.data.pfileUrlSingle] // 需要预览的图片http链接列表
                                    })
                                }
                                if (tag == 1) {
                                    //拼版预览
                                    wx.previewImage({
                                        current: that.data.pfileUrl, // 当前显示图片的http链接
                                        urls: [that.data.pfileUrl] // 需要预览的图片http链接列表
                                    })
                                }
                            },
                        })
                    } else {
                        utils.toast(res.data.msg);
                    }
                })
            }
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        that.setData({
            id: options.id
        })
        that.getIndex(options.id);
        wx.getSystemInfo({
            success: (result) => {
                var model = result.model;
                if (model == 'iPhone X') {
                    that.setData({
                        pdbottom: '68',
                    })
                } else {
                    that.setData({
                        pdbottom: '40'
                    })
                }
            },
        })
    },
    pay(e) {
        var that = this;
        var id = e.currentTarget.dataset.id;
        var amount = e.currentTarget.dataset.amount;
        wx.navigateTo({
            url: '../../print/pay/pay?taskNo=' + id + '&payTypeList=' + JSON.stringify(that.data.payTypeList) + '&price=' + amount + '&once=2&type=oncePay',
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this;
        if (that.data.firstOpen == 1) {
            that.getIndex(that.data.id);
        } else {
            that.setData({
                firstOpen: 1
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },
    jt(e) {
        var orderId = e.currentTarget.dataset.orderid;
        wx.setClipboardData({
            data: orderId,
            success: function (res) {},
        })

    },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    }
})